package cn.com.jinhao.provider.user.service.impl;

import cn.com.jinhao.provider.user.entity.Dept;
import cn.com.jinhao.provider.user.entity.OperDept;
import cn.com.jinhao.provider.user.mapper.CommonMapper;
import cn.com.jinhao.provider.user.mapper.DeptMapper;
import cn.com.jinhao.provider.user.mapper.OperDeptMapper;
import cn.com.jinhao.provider.user.service.DeptService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * HotLine
 * <p>Description: </p>
 *
 * @author ShaneMew
 * @version 1.0
 * @date 2019/11/25
 * @since 2019-11-25
 */

@Service
public class DeptServiceImpl implements DeptService {
    @Resource
    private OperDeptMapper operDeptMapper;
    @Resource
    private DeptMapper deptMapper;
    @Resource
    private CommonMapper commonMapper;

    @Override
    public Dept queryByPrimaryKey(String id) {
            return deptMapper.selectByPrimaryKey(id);
    }

    @Override
    public Dept queryByDeptCode(String code) {

        Example example = new Example(Dept.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("code", code);
        return deptMapper.selectOneByExample(example);
    }

    @Override
    public void add(Dept dept) {
            String code = commonMapper.getCode("ST$DEPT");
            dept.setCode(StringUtils.isBlank(code) ? "0" : String.valueOf(Integer.parseInt(code) + 1));
            deptMapper.insertSelective(dept);
    }

    @Override
    public void update(Dept dept) {
            deptMapper.updateByPrimaryKey(dept);
    }

    @Override
    public void delete(String id) {
            deptMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByDeptCode(String code) {
        Example example = new Example(Dept.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("code", code);
        deptMapper.deleteByExample(example);
    }

    @Override
    public String queryDeptCodeByUserCode(String userCode) {
        Example example = new Example(OperDept.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("stOperCode",userCode);
        OperDept operDept = operDeptMapper.selectOneByExample(example);
        return operDept.getStDeptCode();
    }

    @Override
    public Map<String, Object> queryAll(int pageNum, int pageSize) {
        HashMap<String, Object> map = new HashMap<>();
        if (pageNum != 0 && pageSize != 0) {
            PageHelper.startPage(pageNum, pageSize);
            PageInfo<Dept> pageInfo = new PageInfo<>(deptMapper.selectAll());
            map.put("list", pageInfo);
            map.put("total", pageInfo.getTotal());
            map.put("pageSize", pageInfo.getPageSize());
            map.put("pageNo", pageInfo.getPageNum());
        } else {
            List<Dept> depts = deptMapper.selectAll();
            map.put("list", depts);
            map.put("total", depts.size());
        }
        return map;
    }

    @Override
    public List<Dept> queryByParentDeptCode(String parentDeptCode) {
        Example example = new Example(Dept.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parentDeptCode", parentDeptCode);
        return deptMapper.selectByExample(example);
    }

    @Override
    public String getNameByCode(String code) {
        return deptMapper.getNameByCode(code);
    }
}
